package cn.sort;

import java.util.Arrays;

public class HeapSort {

    public static int[] generatorArray(int size, int value) {
        //初始化一个大小是0-size的数组
        int[] arr = new int[(int) ((size + 1) * Math.random())];
        //生成-value到value之间的随机数
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((value + 1) * Math.random() - (value + 1) * Math.random());
        }
        return arr;
    }

    public static boolean isEqual(int[] arr1, int[] arr2) {
        if (null == arr1 || null == arr2) {
            return false;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static int[] copyArray(int[] arr) {
        int[] help = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            help[i] = arr[i];
        }
        return help;
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;

    }

    public static  void rightSort(int[] arr){
        Arrays.sort(arr);
    }

    public static void heapSort(int[] arr) {
        if (null == arr || arr.length < 2) {
            return;
        }
        //建立大根堆
        for (int i = 0; i < arr.length; i++) {
            heapInsert(arr, i);
        }
        int heapSize = arr.length;
        swap(arr,0,--heapSize);
        //堆调整排序
        while (heapSize > 0) {
            //再次形成大根堆
            heapify(arr,0,heapSize);
            swap(arr,0,--heapSize);
        }
    }

    public static void heapInsert(int[] arr, int index) {
        //如果当前节点的值比父节点大,那么就和父节点交换
        while (arr[index] > arr[(index - 1) / 2]) {
            swap(arr, index, (index - 1) / 2);
            index = (index - 1) / 2;
        }
    }

    public static void heapify(int[] arr, int index, int heapSize) {
        int left = index * 2 + 1;
        while (left < heapSize) {
            int largest = left + 1 < heapSize && arr[left + 1] > arr[left]
                    ?left + 1:left;

            largest = arr[largest] > arr[index]?largest:index;
            if (largest == index) {
                break;
            }
            swap(arr,largest,index);
            index = largest;
            left = index * 2 +1;
        }
    }



    public static void main(String[] args) {
        int size = 20;
        int value = 30;
        int frequence = 5000;
        boolean result = true;
        for (int i = 0; i < frequence; i++) {
            //生成随机数组
            int[] arr = generatorArray(size, value);
            //复制数组
            int[] arr2 = copyArray(arr);
            heapSort(arr);
            rightSort(arr2);
            if (!isEqual(arr, arr2)) {
                result = false;
                for (int j = 0; j < arr.length; j++) {
                    System.out.print(arr[j] + ",");
                }
                break;
            }
        }
        if (result) {
            System.out.println("nice");
        } else {
            System.out.println("fuck");
        }
    }
}
